package com.atguigu.edu.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.common.base.BaseApp;
import com.atguigu.edu.realtime.common.base.BeanToJsonStrMapFunction;
import com.atguigu.edu.realtime.common.bean.TradePaperScoreStatistics;
import com.atguigu.edu.realtime.common.util.DateFormatUtil;
import com.atguigu.edu.realtime.common.util.FlinkSinkUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class DwsExaminePaperScoreStatistics extends BaseApp {
    public static void main(String[] args) {
        new DwsExaminePaperScoreStatistics().start(
                12312,
                4,
                "dws_exam_paper_score_statistics_window",
                "dwd_exam_exam"
        );
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> kafkaStrDS) {
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaStrDS.process(
                new ProcessFunction<String, JSONObject>() {
                    @Override
                    public void processElement(String jsonStr, ProcessFunction<String, JSONObject>.Context context, Collector<JSONObject> collector) throws Exception {
                        if (StringUtils.isNotEmpty(jsonStr)) {
                            JSONObject jsonObj = JSON.parseObject(jsonStr);
                            collector.collect(jsonObj);
                        }
                    }
                }
        );

        SingleOutputStreamOperator<JSONObject> watermarkDS = jsonObjDS.assignTimestampsAndWatermarks(
                WatermarkStrategy.<JSONObject>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<JSONObject>() {
                                    @Override
                                    public long extractTimestamp(JSONObject jsonObject, long l) {
                                        return jsonObject.getLong("ts")*1000;
                                    }
                                }
                        )
        );

        KeyedStream<JSONObject, String> paperDS = watermarkDS.keyBy(jsonObj -> jsonObj.getString("paper_id"));
        //paperDS.print();
        SingleOutputStreamOperator<TradePaperScoreStatistics> beanDS = paperDS.process(
                new KeyedProcessFunction<String, JSONObject, TradePaperScoreStatistics>() {
                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TradePaperScoreStatistics>.Context context, Collector<TradePaperScoreStatistics> collector) throws Exception {
                        Double score = jsonObject.getDouble("score");
                        String paperId = jsonObject.getString("paper_id");
                        String paperTitle = jsonObject.getString("paper_title");
                        Long ts = jsonObject.getLong("ts");
                        TradePaperScoreStatistics bean = TradePaperScoreStatistics.builder()
                                .paper_id(paperId)
                                .paper_title(paperTitle)
                                .from70to90(0L)
                                .less60(0L)
                                .from60to70(0L)
                                .above90(0L)
                                .ts(ts)
                                .build();
                        if (score < 60.0 ) {
                            bean.setLess60(1L);
                        }
                        if (70.0 > score && score >= 60.0) {
                            bean.setFrom60to70(1L);
                        }
                        if (90.0 > score && score >= 70.0) {
                            bean.setFrom70to90(1L);
                        }
                        if (score >= 90.0){
                            bean.setAbove90(1L);
                        }
                        collector.collect(bean);
                    }
                }
        );

        //beanDS.print();

        KeyedStream<TradePaperScoreStatistics, String> keyedDS = beanDS.keyBy(TradePaperScoreStatistics::getPaper_id);

        //keyedDS.print();

        WindowedStream<TradePaperScoreStatistics, String, TimeWindow> windowDS = keyedDS.window(TumblingEventTimeWindows.of(Time.seconds(30)));


        SingleOutputStreamOperator<TradePaperScoreStatistics> reduceDS = windowDS.reduce(
                new ReduceFunction<TradePaperScoreStatistics>() {
                    @Override
                    public TradePaperScoreStatistics reduce(TradePaperScoreStatistics v1, TradePaperScoreStatistics v2) throws Exception {
                        v1.setLess60(v1.getLess60() + v2.getLess60());
                        v1.setFrom60to70(v1.getFrom60to70() + v2.getFrom60to70());
                        v1.setFrom70to90(v1.getFrom70to90() + v2.getFrom70to90());
                        v1.setAbove90(v1.getAbove90() + v2.getAbove90());
                        return v1;
                    }
                },
                new WindowFunction<TradePaperScoreStatistics, TradePaperScoreStatistics, String, TimeWindow>() {
                    @Override
                    public void apply(String jsonStr, TimeWindow timeWindow, Iterable<TradePaperScoreStatistics> iterable, Collector<TradePaperScoreStatistics> collector) throws Exception {
                        TradePaperScoreStatistics orderBean = iterable.iterator().next();
                        orderBean.setStt(DateFormatUtil.tsToDateTime(timeWindow.getStart()));
                        orderBean.setEdt(DateFormatUtil.tsToDateTime(timeWindow.getEnd()));
                        orderBean.setCurDate(DateFormatUtil.tsToDate(timeWindow.getStart()));
                        collector.collect(orderBean);
                    }
                }
        );

        reduceDS.print();
        reduceDS.map(new BeanToJsonStrMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink("dws_exam_paper_score_statistics_window"));

    }
}
